/**
 * @author Johniak
 */
$(document).ready(function () {
	getEventsAjax();
	$('#show_event_button').click(showEvent);
	$('#show_add_event_modal_button').click(showAddEventModal);
	$('#add_event_button').click(addEventAjax);
});

function getEventsAjax() {
	$.ajax({
		url: "/api/events/get/by/hooligan_id",
		type: 'POST',
		data: {
			id: $('#hool_id').val(),
			csrfmiddlewaretoken: $.cookie('csrftoken')
		},
		success: function (response) {
			$('#events_table_body').html('');
			var events = eval(response);
			for (i = 0; i < events.length; i++) {
				$('#events_table_body').append('<tr>' + '<td><input class="tabradio" type="radio" name="tab1" value="' + events[i].id + '" /></td>' +
					'<td>' + events[i].id + '</td>' +
					'<td class="name">' + events[i].offenseType + '</td>' +
					'<td class="lastname">' + events[i].username + '</td>' +
					'<td class="coords">' + events[i].coordinatesWidth + ',' + events[i].coordinatesHeight + '</td>' +
					'<td class="date">' + events[i].date + '</td>' + '</tr>');
			}
		}
	});
}
function showEvent() {
	if (typeof getSelectedId() === 'undefined') {
		alert("You must select row.");
		return;
	}
	window.location.href = "/events/show/" + getSelectedId();
}
function getSelectedId() {
	return $('input[name=tab1]:checked').val()
}

function showAddEventModal() {
	$('#add_event_modal').modal('show');
}

function addEventAjax() {
	var formData = new FormData(document.getElementById('add_event_form'));
	$.ajax({
		url: '/api/hools/add/event',
		type: 'POST',
		data: formData,
		cache: false,
		contentType: false,
		processData: false,
		success: function () {
			$('#add_event_modal').modal('hide');
			getEventsAjax();
		}
	});
}